﻿
using Microsoft.Practices.Prism.Events;
using compshop.Infrastructure;

namespace compshop.Modules.ClientOrderEdit
{
    public class EventsManager
    {
        private IEventAggregator eventAggregator;
        private SubscriptionToken subscriptionToken;
        private bool IsActive = false;

        public Presenter.ClientOrderEditPresenter clientOrderEditPresenter { get; set; }

        public IEventAggregator EventAggregator
        {
            get { return eventAggregator; }
        }     

        public EventsManager(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
        }

        public void Subscribe()
        {
			ModuleActivityEvent moduleActivityEvent = eventAggregator.GetEvent<ModuleActivityEvent>();
            moduleActivityEvent.Subscribe(this.ActivityProcessor);

            ClientOrderSelectedEvent selectedEvent = eventAggregator.GetEvent<ClientOrderSelectedEvent>();
            subscriptionToken = selectedEvent.Subscribe(clientOrderEditPresenter.SetSelectedClientOrder, ThreadOption.UIThread, false, ActivityFilter);

            AddToBasketEvent addEvent = eventAggregator.GetEvent<AddToBasketEvent>();
            subscriptionToken = addEvent.Subscribe(clientOrderEditPresenter.AddProductToOrder, ThreadOption.UIThread, false, ActivityFilter);
        }

       private void ActivityProcessor(ModuleActivityState state)
        {
            if (state.ActivateModule == RegionNames.ClientOrderEditContent)
            {
                if (!IsActive)
                {
                    // activation code place here
                    IsActive = true;
                }
            }
            else
            {
                if (state.DeactivateAll)
                {
                    if (IsActive)
                    {
                        // deactivation code place here
                        IsActive = false;
                    }
                }
            }

        }

        public bool ActivityFilter(int a)
        {
            return IsActive;
        }
    }
}
